package com.example.mobile2032021606168xmy0326homework;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tvMainTitle;
    private EditText extTextMainShow;
    private Button btnMainClear;
    boolean clear_flag;
    private Button btnMainMul;
    private Button btnMainDiv;
    private Button btnMainBack;
    private Button btnMainNum7;
    private Button btnMainNum8;
    private Button btnMainNum9;
    private Button btnMainSub;
    private Button btnMainNum4;
    private Button btnMainNum5;
    private Button btnMainNum6;
    private Button btnMainPer;
    private Button btnMainNum1;
    private Button btnMainNum2;
    private Button btnMainNum3;
    private Button btnMainAdd;
    private Button btnMainNum;
    private Button tvMainPoint;
    private Button tvMainEqual;
    String msga, getMsga;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        btnMainNum.setOnClickListener(this);
        btnMainNum1.setOnClickListener(this);
        btnMainNum2.setOnClickListener(this);
        btnMainNum3.setOnClickListener(this);
        btnMainNum4.setOnClickListener(this);
        btnMainNum5.setOnClickListener(this);
        btnMainNum6.setOnClickListener(this);
        btnMainNum7.setOnClickListener(this);
        btnMainNum8.setOnClickListener(this);
        btnMainNum9.setOnClickListener(this);
        btnMainClear.setOnClickListener(this);
        btnMainMul.setOnClickListener(this);
        btnMainDiv.setOnClickListener(this);
        btnMainBack.setOnClickListener(this);
        btnMainPer.setOnClickListener(this);
        btnMainAdd.setOnClickListener(this);
        btnMainSub.setOnClickListener(this);
        tvMainPoint.setOnClickListener(this);
        tvMainEqual.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Button btn = (Button) v;
        int id = btn.getId();
        String input = extTextMainShow.getText().toString();
        switch (id) {
            case R.id.btn_main_num:
                extTextMainShow.append("0");
                break;
            case R.id.btn_main_num1:
                extTextMainShow.append("1");
                break;
            case R.id.btn_main_num2:
                extTextMainShow.append("2");
                break;
            case R.id.btn_main_num3:
                extTextMainShow.append("3");
                break;
            case R.id.btn_main_num4:
                extTextMainShow.append("4");
                break;
            case R.id.btn_main_num5:
                extTextMainShow.append("5");
                break;
            case R.id.btn_main_num6:
                extTextMainShow.append("6");
                break;
            case R.id.btn_main_num7:
                extTextMainShow.append("7");
                break;
            case R.id.btn_main_num8:
                extTextMainShow.append("8");
                break;
            case R.id.btn_main_num9:
                extTextMainShow.append("9");
                break;
            case R.id.btn_main_point:
                if (clear_flag){
                    clear_flag=false;
                    extTextMainShow.setText("");
                }
                extTextMainShow.setText(input+((Button)v).getText());
                break;
            case R.id.btn_main_clear:
                // extTextMainShow.setText(null);
                if (clear_flag) {
                    clear_flag = false;
                    input = "";
                    extTextMainShow.setText("");
                } else if (input != null || !input.equals("")) {
                    extTextMainShow.setText(input.substring(0, input.length() - 1));
                }
                break;
            case R.id.btn_main_back:
                String msg = extTextMainShow.getText().toString();
                int length = msg.length();
                if (Pattern.matches("^=[0-9].*", msg)) {
                    extTextMainShow.setText("0");

                } else {
                    //if (msg.length() > 1) {
                    // msg.substring(0, msg.length());
                    if (length > 0) {
                        String word = msg.substring(length - 1, length);
                        if (word.equals(".")) ;
                        if (word.equals("+") || word.equals("-") || word.equals("*") || word.equals(("/")))
                            extTextMainShow.setText(msg.substring(0, length - 1));
                    }
                }

                break;
            case R.id.btn_main_add:
                msga = extTextMainShow.getText().toString();
                int a = Integer.valueOf(msga);
                extTextMainShow.append("+");
                break;


            case R.id.btn_main_div:
            // extTextMainShow.append("/");
            if(clear_flag) {
        clear_flag = false;
        input = "";
        extTextMainShow.setText("");
    }
                extTextMainShow.setText(input +" "+((Button)v).getText() +" ");
                break;
            case R.id.btn_main_mul:
            extTextMainShow.append("*");
                break;
            case R.id.btn_main_sub:
            extTextMainShow.append("-");
                break;
            case R.id.tv_main_equal:
                extTextMainShow.append("=");
                getResult();
                break;
}
}
  private void getResult() {
      String exp = extTextMainShow.getText().toString();
      if (exp == null || exp.equals("")) {
          return;
      }
      if (!exp.contains(" ")) {
          return;
      }
      if (clear_flag) {
          clear_flag = false;
          return;
      }
      clear_flag = true;
      double result = 0;
      String s1 = exp.substring(0, exp.indexOf(" "));
      String op = exp.substring(exp.indexOf(" ") + 1, exp.indexOf(" ") + 2);
      String s2 = exp.substring(exp.indexOf(" ") + 3);

      if (!s1.equals(" ") && !s2.equals("")) {
          double d1 = Double.parseDouble(s1);
          double d2 = Double.parseDouble(s2);
          if (op.equals("+")) {
              result = d1 + d2;
          } else if (op.equals("-")) {
              result = d1 - d2;
          } else if (op.equals("*")) {
              if (d2 == 0) {
                  result = 0;
              } else {
                  result = d1 / d2;
              }
          }
          if (!s1.contains(".") && !s2.contains(".") && !op.equals("/")) {
              int r = (int) result;
              extTextMainShow.setText(r + "");
          } else {
              extTextMainShow.setText(result + "");
          }
      } else if (!s1.equals("") && s2.equals("")) {
          extTextMainShow.setText(exp);
      } else if (s1.equals("") && !s2.equals("")) {
          double d2 = Double.parseDouble(s2);
          if (op.equals("+")) {
              result = 0 + d2;
          } else if (op.equals("-")) {
              result = 0 - d2;
          } else if (op.equals("*")) {
              result = 0;
          } else if (op.equals("/")) {

          }
          if (!s1.contains(".") && !s2.contains(".")) {
              int r = (int) result;
              extTextMainShow.setText(r + "");
          } else {
              extTextMainShow.setText(result + "");
          }
          }else{
              extTextMainShow.setText("");
          }

      }
      private void initView () {
          tvMainTitle = (TextView) findViewById(R.id.tv_main_title);
          extTextMainShow = (EditText) findViewById(R.id.edtText_main_show);
          btnMainClear = (Button) findViewById(R.id.btn_main_clear);
          btnMainMul = (Button) findViewById(R.id.btn_main_mul);
          btnMainDiv = (Button) findViewById(R.id.btn_main_div);
          btnMainBack = (Button) findViewById(R.id.btn_main_back);
          btnMainNum7 = (Button) findViewById(R.id.btn_main_num7);
          btnMainNum8 = (Button) findViewById(R.id.btn_main_num8);
          btnMainNum9 = (Button) findViewById(R.id.btn_main_num9);
          btnMainSub = (Button) findViewById(R.id.btn_main_sub);
          btnMainNum4 = (Button) findViewById(R.id.btn_main_num4);
          btnMainNum5 = (Button) findViewById(R.id.btn_main_num5);
          btnMainNum6 = (Button) findViewById(R.id.btn_main_num6);
          btnMainPer = (Button) findViewById(R.id.btn_main_per);
          btnMainNum1 = (Button) findViewById(R.id.btn_main_num1);
          btnMainNum2 = (Button) findViewById(R.id.btn_main_num2);
          btnMainNum3 = (Button) findViewById(R.id.btn_main_num3);
          btnMainAdd = (Button) findViewById(R.id.btn_main_add);
          btnMainNum = (Button) findViewById(R.id.btn_main_num);
          tvMainPoint = (Button) findViewById(R.id.btn_main_point);
          tvMainEqual = (Button) findViewById(R.id.tv_main_equal);

      }
  }